package com.hns.can.master.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.hns.can.master.vo.*;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysRoleEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.service.HomeCaptainWholeService;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.security.AppUserDetailsImpl;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.SearchConditionVo;

/**
 * 车队长首页controller
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/homeCaptain")
public class HomeCaptainWholeController extends BaseController {

	@Autowired
	private HomeCaptainWholeService homeCaptainWholeService;
	
	@Autowired
	private CommonInitParamService commonInitParamService;
	
	
	/*
	 *TODO 首页部分 
	 */
	
	/**
	 * 获取首页指标
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("getHomeIndex")
	@ResponseBody
	public Object getHomeIndex(HomePageParamVo params){
		initHomeCaptainParams(params);
		if("030101".equals(params.getFuelTypeTot())){
			params.setFuelTypePer("050101");
		}else if("030103".equals(params.getFuelTypeTot())){
			params.setFuelTypePer("050103");
		}else if("030102".equals(params.getFuelTypeTot())){
			params.setFuelTypePer("050102");
		}
		List<HomeCaptainMainVo> list = homeCaptainWholeService.getHomeIndex(params);
		HomeCaptainMainVo vo = null;
		if(PubUtil.isNotEmpty(list)){
			vo = list.get(0);
		}
		return this.getReturnResult(vo);
	}
	
	
	/**
	 * 获取首页对比指标
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("getCompareIndex")
	@ResponseBody
	public Object getCompareIndex(HomePageParamVo params){
		initHomeCaptainParams(params);
		GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
		if("02".equals(params.getDateType())){
			cal.add(Calendar.DATE, -2);
    		params = (HomePageParamVo)DateTool.getDayDuring(params, cal.getTime());
		}else if("03".equals(params.getDateType())){
			params = (HomePageParamVo)getLastSevenDays(params);
		}else if("04".equals(params.getDateType())){
			cal.add(Calendar.MONTH, -1);
    		params = (HomePageParamVo)DateTool.getMonthDuring(params, cal.getTime());
		}else if("05".equals(params.getDateType())){
			cal.add(Calendar.MONTH, -2);
    		params = (HomePageParamVo)DateTool.getMonthDuring(params, cal.getTime());
		}else if("06".equals(params.getDateType())){
			cal.add(Calendar.YEAR, -1);
    		params = (HomePageParamVo)DateTool.getYearDuring(params, cal.getTime());
		}else if("07".equals(params.getDateType())){
			cal.add(Calendar.YEAR, -2);
    		params = (HomePageParamVo)DateTool.getYearDuring(params, cal.getTime());
		}
		List<HomeCaptainMainVo> list = homeCaptainWholeService.getCompareIndex(params);
		HomeCaptainMainVo vo = null;
		if(PubUtil.isNotEmpty(list)){
			vo = list.get(0);
		}
		return this.getReturnResult(vo);
	}
	
	private static SearchConditionVo getLastSevenDays(SearchConditionVo vo){
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    	Calendar now = Calendar.getInstance();
    	Date begin = DateUtils.addDays(now.getTime(), -14);    
    	Calendar cal = Calendar.getInstance();
        cal.setTime(begin);    
        vo.setBeginTime(fmt.format(cal.getTime()));
        Date end = DateUtils.addDays(now.getTime(), -8);    
        cal.setTime(end);    
        vo.setEndTime(fmt.format(cal.getTime()) + " 23:59:59");
        return vo;
    }
	
	
	/**
	 * 获取线路下所有车辆最后位置
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getLineCarLastPosition")
	@ResponseBody
	public Object getLineCarLastPosition(HomePageParamVo params) throws Exception{
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getLineCarLastPosition(params);
		return this.getReturnResult(list);
	}
	
	
	/**
	 * 获取线路下车辆指定时间内报警信息
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getRecentWarnInfo")
	@ResponseBody
	public Object getRecentWarnInfo(HomePageParamVo params) throws Exception {
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getRecentWarnInfo(params);
		return this.getReturnResult(list);
		
	}
	
	
	/*
	 * TODO 安全管理部分
	 */
	
	/**
	 * 获取能耗故障排名（前五名，后五名）
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getFaultsRanking")
	@ResponseBody
	public Object getFaultsRanking(HomePageParamVo params) throws Exception{
		initHomeCaptainParams(params);
		ArrayList<SysRoleEntity> roleList = new ArrayList<SysRoleEntity>(this.getCurrentUser().getSet_role());
		params.setRoleList(roleList);
		List list = homeCaptainWholeService.getFaultsRanking(params);
		return this.getReturnResult(list);
	}
	
	/**
	 * 线路平均故障数
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("getFaultsRankCount")
	@ResponseBody
	public Object getFaultsRankCount(HomePageParamVo params) throws Exception{
		initHomeCaptainParams(params);
		ArrayList<SysRoleEntity> roleList = new ArrayList<SysRoleEntity>(this.getCurrentUser().getSet_role());
		params.setRoleList(roleList);
		List<HomeCaptainFaultsRankingVo> list = homeCaptainWholeService.getFaultsRankCount(params);
		HomeCaptainFaultsRankingVo homeCaptainFaultsRankingVo = null;
		if(PubUtil.isNotEmpty(list)){
			homeCaptainFaultsRankingVo = list.get(0);
		}
		return this.getReturnResult(homeCaptainFaultsRankingVo);
	}
	
	/**
	 * 故障和不安全行为饼图数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping(value={"getFaultsChart","getUnsafeBhvChart"})
	@ResponseBody
	public Object getHarmfulTypeRanking(HomePageParamVo params){
		initHomeCaptainParams(params);
		List<HomeCaptainFaultRankChartVo> list = homeCaptainWholeService.harmfulTypeRanking(params);
		return this.getReturnResult(list);
	}
	
	
	/*
	 * TODO 驾驶员管理
	 */
	
	/**
	 * 车队长首页驾驶员平均数据
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("getDriverUnsafeRankCount")
	@ResponseBody
	public Object getDriverUnsafeRankCount(HomePageParamVo params){
		initHomeCaptainParams(params);
		List<HomeCaptainUnsafeRankCountVo> list = homeCaptainWholeService.getDriverUnsafeRankCount(params);
		return this.getReturnResult(list);
	}
	
	
	/**
	 * 车队长首页驾驶员排名（前五名，后五名）
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getDriverUnsafeRanking")
	@ResponseBody
	public Object getDriverUnsafeRanking(HomePageParamVo params) throws Exception{
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getDriverUnsafeRanking(params);
		return this.getReturnResult(list);
	}
	
	
	/*
	 * TODO 能耗管理部分 
	 */
	
	/**
	 * 查询能耗排名
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getEnergyRanking")
	@ResponseBody
	public Object getEnergyRanking(HomePageParamVo params) throws Exception{
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getEnergyRanking(params);
		return this.getReturnResult(list);
	}
	
	/**
	 * 查询平均能耗值
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getAvgEnergyVal")
	@ResponseBody
	public Object getAvgEnergyVal(HomePageParamVo params) throws Exception{
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getAvgEnergyVal(params);
		return this.getReturnResult(list);
	}
	
	/**
	 * 能耗趋势值
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getEnergyTendencyCharts")
	@ResponseBody
	public Object getEnergyTendencyCharts(HomePageParamVo params){
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getEnergyTendencyCharts(params);
		return this.getReturnResult(list);
	}
	
	/**
	 * 能耗结论数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getEnergyConclude")
	@ResponseBody
	public Object getEnergyConclude(HomePageParamVo params){
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getEnergyConclude(params);
		return this.getReturnResult(list);
	}
	
	/**
	 * 能耗模型饼图数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getEnergyStatusPieChart")
	@ResponseBody
	public Object getEnergyStatusPieChart(HomePageParamVo params){
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getEnergyStatusPieChart(params);
		return this.getReturnResult(list);
	}	
	
	/**
	 * 能耗问题和建议部分数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getEnerMultiInfo")
	@ResponseBody
	public Object getEnerMultiInfo(HomePageParamVo params){
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getEnerMultiInfo(params);
		return this.getReturnResult(list);
	}
	
	/*
	 *成本管理
	 */
	
	/**
	 * 加载安全首页
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("getCaptainFinanceData")
	@ResponseBody
	public Object getCaptainFinanceData(HomePageParamVo params){
		initHomeCaptainParams(params);
		Pagination page = homeCaptainWholeService.getCaptainFinanceData(params);
		GridPage grid = new GridPage(page);
		return this.getReturnResult(grid);
	}
	
	
	
	/**
	 * 获取用户线路资源
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("getCaptainLineInfo")
	@ResponseBody
	public Object getCaptainLineInfo(){
		List list = homeCaptainWholeService.getCaptainLineInfo();
		return this.getReturnResult(list);
	}
	
	/**
	 * 格式化参数
	 * @param params
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	private void initHomeCaptainParams(HomePageParamVo params){
		AppUserDetailsImpl currentUser = this.getCurrentUser();
		if(PubUtil.isNotEmpty(currentUser)){
			SysUserEntity user = currentUser.getUser();
			params.setCorpId(user.getCorpId());
			params.setUserId(user.getId());
			commonInitParamService.dateType2DateTime(params, params.getDateType());
			commonInitParamService.initParamsLimitLine(params, user, null);
		}else{
			throw new BusinessException(MsgType.ERROR.getFlag(),"未知用户登录");
		}
	}

	@RequestMapping("getLastMiniteWarnList")
	@ResponseBody
	public Object getLastMiniteWarnList(HomePageParamVo params){
		initHomeCaptainParams(params);
		List list = homeCaptainWholeService.getLastMiniteWarnList(params);
		return this.getReturnResult(list);
	}

	@RequestMapping("getRecentlyWarn")
	@ResponseBody
	public Object getRecentlyWarn(HomePageParamVo params) throws Exception{
		HomeCaptainMainMapVo vo = homeCaptainWholeService.getRecentlyWarn(params);
		return this.getReturnResult(vo);
	}
}
